﻿#include "qslog_widget.h"


using namespace QsLogging;

QsLogWidget::QsLogWidget(QObject *parent)
    :QObject(parent)
{
    logModel = new QsLogTableModel(this);
    logDelegate = new QsLogItemDelegate(this);
    logView = Q_NULLPTR;
}
void QsLogWidget::setTableView(QTableView *tableview)
{
    if(tableview && logModel && logDelegate)
    {
        logView = tableview;
        logView->setModel(logModel);

        //logView->setEditTriggers(QAbstractItemView::NoEditTriggers);
        //logView->setSelectionMode(QAbstractItemView::ExtendedSelection);
        //logView->setSelectionBehavior(QAbstractItemView::SelectRows);
        logView->setItemDelegateForColumn(0,logDelegate);
        logView->setColumnWidth(0,100);
        logView->setColumnWidth(1,300);
    }
}
void QsLogWidget::disAll()
{
    if(logModel)
        logModel->setDisLevel(QsLogging::DebugLevel);
}

void QsLogWidget::disInfo()
{
    if(logModel)
        logModel->setDisLevel(QsLogging::InfoLevel);
}
void QsLogWidget::disWarn()
{
    if(logModel)
        logModel->setDisLevel(QsLogging::WarnLevel);
}
void QsLogWidget::disFatal()
{
    if(logModel)
        logModel->setDisLevel(QsLogging::FatalLevel);
}

void QsLogWidget::addLog(QString message)
{
    logModel->logSlot(message,QsLogging::FatalLevel);
}
void QsLogWidget::setHistoryData(QStringList& his)
{
    logModel->setHistoryData(his);
}
QsLogItemDelegate::QsLogItemDelegate(QObject *parent)
    :QStyledItemDelegate(parent)
{
}
void QsLogItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const
{
    if(index.column() == 0)
    {
        if(option.state & QStyle::State_Selected)
            painter->fillRect(option.rect, option.palette.highlight());

        QString str = index.model()->data(index,Qt::DisplayRole).toString();
        if(!str.isEmpty())
        {
            QPixmap images;
            int level = QsLogging::Logger::levelFromLogMessage(str);
            if(level == QsLogging::InfoLevel)
            {
                QPixmap image;
                image.load(":/resource/images/log/info.png");
                images = image.scaledToHeight(option.rect.height() - 8);
                painter->drawPixmap(option.rect.x(),option.rect.y() + 4,images.rect().width(),images.rect().height(),images);
            }
            else if(level == QsLogging::WarnLevel)
            {
                QPixmap image;
                image.load(":/resource/images/log/warn.png");
                images = image.scaledToHeight(option.rect.height() - 8);
                painter->drawPixmap(option.rect.x(),option.rect.y() + 4,images.rect().width(),images.rect().height(),images);
            }
            else if(level == QsLogging::FatalLevel)
            {
                QPixmap image;
                image.load(":/resource/images/log/fatal.png");
                images = image.scaledToHeight(option.rect.height() - 8);
                painter->drawPixmap(option.rect.x(),option.rect.y() + 4,images.rect().width(),images.rect().height(),images);

            }

            QString removeLevelStr = QsLogging::Logger::levelRemoveMessage(str);
            if(!removeLevelStr.isEmpty())
            {

                if(!images.isNull())
                {
                    QRect rectr = option.rect;
                    rectr.setX(rectr.x() + images.rect().width()+5);
                    QStyleOptionViewItem optcopy = option;
                    initStyleOption(&optcopy, index);
                    optcopy.state &= ~QStyle::State_HasFocus;
                    optcopy.text = removeLevelStr;
                    optcopy.rect = rectr;
                    QStyle *style = option.widget ? option.widget->style() : QApplication::style();
                    style->drawControl(QStyle::CE_ItemViewItem, &optcopy, painter, option.widget);

                    optcopy.state = option.state;
                    if (optcopy.state & QStyle::State_HasFocus) {
                       QStyleOptionFocusRect o;

                       o.rect = option.rect;
                       o.state |= QStyle::State_KeyboardFocusChange;
                       o.state |= QStyle::State_Item;
                       QPalette::ColorGroup cg = (optcopy.state & QStyle::State_Enabled)
                                     ? QPalette::Normal : QPalette::Disabled;
                       o.backgroundColor = optcopy.palette.color(cg, (optcopy.state & QStyle::State_Selected)
                                                    ? QPalette::Highlight : QPalette::Window);
                       style->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter, option.widget);
                   }

                }
                else
                {

                    QStyleOptionViewItem optcopy = option;
                    initStyleOption(&optcopy, index);
                    optcopy.text = removeLevelStr;
                    QStyle *style = option.widget ? option.widget->style() : QApplication::style();
                    style->drawControl(QStyle::CE_ItemViewItem, &optcopy, painter, option.widget);
                }
            }
        }
    }
//    else
//    {
//        return QStyledItemDelegate::paint(painter, option, index);
//    }

}
